/*
 * @lc app=leetcode.cn id=14 lang=golang
 *
 * [14] 最长公共前缀
 */

// @lc code=start
func longestCommonPrefix(strs []string) string {
	l := len(strs)
	if l == 1 {
		return strs[0]
	}
	var pix = strs[0]
	var str string
	var length int = len(pix)
	for i := 1; i < l; i++ {
		str = strs[i]
		if len(str) < length {
			length = len(str)
		}
		for j := 0; j < length; j++ {
			if str[j] != pix[j] {
				length = j
				break
			}
		}
		if length == 0 {
			return ""
		}
		if length < len(pix) {
			pix = pix[0:length]
		}
	}
	return pix
}

// @lc code=end

// Accepted
// 123/123 cases passed (0 ms)
// Your runtime beats 100 % of golang submissions
// Your memory usage beats 30.22 % of golang submissions (2.3 MB)